<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="dataArr" :props="defaultProps" default-expand-all :expand-on-click-node="false">
        <!-- 节点结构-->
        <!-- 用插槽拿到el-tree传过来的数据-->
        <template v-slot="{ data }">
          <el-row type="flex" justify="space-between" align="middle" style="width: 100%;height:40px ">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manage">{{ data.managerName }}</span>
              <!--$event:事件默认的实参，就是啥也不传的默认实参-->
              <el-dropdown @command="handleCommand($event,data.id)">
              <span class="el-dropdown-link">
                操作<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 放置弹层-->
    <!-- .sync 表示自动监听子组件事件，接收子组件的事件 简化父组件的接收-->
    <AddDialog ref="addDialog" @updateDepartment="getList" :showDialog.sync="showDialog"
               :currentNodeId="currentNodeId"></AddDialog>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import {getDepartmentList, delDepart} from '@/api/department'
import {toTreeDate} from '@/utils/index'
import AddDialog from './components/add-dialog'

export default {
  name: 'Department',
  components: {
    AddDialog
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  data() {
    return {
      showDialog: false,
      dataArr: [], // 数据属性
      defaultProps: {
        label: 'name', // 显示的字段的名字
        children: 'children' // 读取子节点的字段名
      },
      currentNodeId: null
    }
  },
  created() {
    // this.$store.dispatch('user/getUserInfo')
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getDepartmentList()
      this.dataArr = toTreeDate(res, 0)
    },
    handleCommand(type, id) {
      if (type === 'add') {
        // 添加子部门
        this.showDialog = true
        this.currentNodeId = id
      } else if (type === 'edit') {
        // 编辑
        this.showDialog = true
        this.currentNodeId = id // 记录id 后面要用他获取数据
        // 更新props---异步动作
        // 要在子组件获取数据 父组件调用子组件方法 ref获取子组件实例拿到子组件的this
        // 调用子组件方法---同步动作
        this.$nextTick(() => {
          this.$refs.addDialog.getDepartDetail()
        })
      } else {
        // 删除
        this.$confirm('确定要删除吗?', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await delDepart(id)
          this.$message({
            type: 'success',
            message: '删除部门成功!'
          })
          this.getList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}

.tree-manage {
  display: inline-block;
  width: 100px;
  margin: 10px;
}
</style>
